﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeatherStationSys.Interface;

namespace WeatherMonitoringSystem
{
    public class HiloDataImpProxy : IHiLoData
    {
        private IPersistent _iPersistent;
        private HiloDataImp _hiloDataImp;
        private string _type;
        private string _storageKey;
        
        /// <summary>
        /// Ctor
        /// </summary>
        public HiloDataImpProxy(StationTookit st, string type, DateTime theDate, double init, long initTime)
        {
            _iPersistent = st.GetPersistent();
            _type = type;
            _storageKey = CalculateStorageKey(theDate);
            IHiLoData iHiLoData = _iPersistent.Retrieve<IHiLoData>(_storageKey);
            CurrentReading(init, initTime);
        }
  
        /// <summary>
        /// 得到最大值
        /// </summary>
        /// <returns></returns>
        public double GetHeightValue()
        {
            return _hiloDataImp.GetHeightValue();
        }

        /// <summary>
        /// 得到最大值的时间
        /// </summary>
        /// <returns></returns>
        public long GetHeightTime()
        {
            return _hiloDataImp.GetHeightTime();
        }

        /// <summary>
        /// 得到最小值的时间
        /// </summary>
        /// <returns></returns>
        public long GetLowerTime()
        {
            return _hiloDataImp.GetLowerTime();
        }

        /// <summary>
        /// 得到最小值
        /// </summary>
        /// <returns></returns>
        public double GetLowerValue()
        {
            return _hiloDataImp.GetLowerValue();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string CalculateStorageKey(DateTime theDate)
        {
            string key = _type + theDate.ToString("yyyyMMdd");

            return key;
        }

        /// <summary>
        /// 当前温度
        /// </summary>
        /// <param name="value">当前值</param>
        /// <param name="time">当前时间</param>
        public bool CurrentReading(double currentValue, long time)
        {
            bool isChanged = false;
            isChanged = _hiloDataImp.CurrentReading(currentValue, time);
            if (isChanged)
            {
                Store();
            }

            return isChanged;
        }

        /// <summary>
        /// 储存数据
        /// </summary>
        public void Store()
        {
            _iPersistent.Store<HiloDataImpProxy>(_storageKey, this);
        }

        public void NewDay(double initial, long time)
        {
            Store();
            _hiloDataImp.NewDay(initial, time);
             _storageKey = CalculateStorageKey(new DateTime());
            Store();
        }

        /// <summary>
        /// 老版本持久化数据
        /// </summary>
        /// <param name="value">当前值</param>
        /// <param name="time">当前时间</param>
        public void DeformedVeiosnCurrentReading(double currentValue, long time)
        {
            throw new NotImplementedException();
        }
    }
}
